package com.service.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @program: sch-diy-parent
 * @description:
 * @author: sch
 * @create: 2021-01-16 21:03
 **/
public class ServerSocketMyThread {
    public static void main(String[] args) throws IOException {
        //初始化服务端socket并绑定9999端口
        ServerSocket serverSocket = new ServerSocket(9999);
        //等待客户端连接
        Socket socket = serverSocket.accept();

        new Thread(new Runnable() {
            @Override
            public void run() {
                //获取输入流
                BufferedReader br = null;
                try {
                    br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "utf-8"));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //读取一行数据
                String line = null;
                //循环读
                while (true) {
                    try {
                        if (!((line = br.readLine()) != null)) {
                            break;
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    //打印
                    System.out.println("客户端信息:" + line);
                }
            }
        }).start();


    }

}
